package org.gitos.cloud.comet.conf;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import org.gitos.cloud.domain.comm.Constant;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

/**
 * NettyConfig
 *
 * @author purely
 * @date 2015/12/4
 */
@Configuration
public class NettyConfig {
    @Bean(name = {"serverBootstrap"})
    @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
    public ServerBootstrap serverBootstrap() {
        return new ServerBootstrap();
    }

    @Value("${" + Constant.BOSS_THREAD + "}")
    private String bossThread;
    @Value("${" + Constant.WORK_THREAD + "}")
    private String workThread;

    @Bean(name = {"boss"})
    public EventLoopGroup boss() {
        return new NioEventLoopGroup(Integer.valueOf(bossThread));
    }

    @Bean(name = {"tcpWorker"})
    public EventLoopGroup tcpWorker() {
        int numWorkThread = Integer.valueOf(workThread);
        int processors = numWorkThread;
        if (numWorkThread == 0) {
            processors = Runtime.getRuntime().availableProcessors() * 2;
        }
        return new NioEventLoopGroup(processors);
    }

}
